package minformer;
import javax.microedition.lcdui.*;


public class OptionsForm implements CommandListener
{
	private String[] colorNames = new String[]{"Чёрный", "Серый", "С-серый", "Белый" };
	private int[] colorValues = new int[] {0x000000, 0x606060, 0xC0C0C0, 0xFFFFFF };
	private int[] reqIntervals = new int[] {1, 2, 4, 6, 12};
	
    private Form form;
    private TextField cityNumber;
    private ChoiceGroup other;
    private ChoiceGroup textColor;
    private ChoiceGroup backColor;
    private ChoiceGroup reqInterval;
    private ChoiceGroup viewMode;
    private Command cmdSave = new Command("Сохранить", Command.OK, 1);
    private Command cmdCancel = new Command("Отменить", Command.CANCEL, 2);
    
    public OptionsForm()
    {
    	form = new Form("Настройки");
    	form.addCommand(cmdSave);
    	form.addCommand(cmdCancel);
    	form.setCommandListener(this);
    }
    
    private void showColor(ChoiceGroup choice, int value)
    {
    	choice.deleteAll();
    	
    	for (int i = 0; i < colorNames.length; i++) 
    		choice.append(colorNames[i], null);
    	
    	for (int i = 0; i < colorValues.length; i++) 
    		if (colorValues[i] == value) choice.setSelectedIndex(i, true); 
    }
    
    private int getColor(ChoiceGroup choice, int defValue)
    {
    	int index = choice.getSelectedIndex(); 
    	if (index == -1) return defValue;
    	return colorValues[index];
    }
    
    public void execute(Display display)
    {
    	cityNumber = new TextField("Код города", Integer.toString(Options.cityNum), 32, TextField.NUMERIC);
    	form.append(cityNumber);
    	
    	viewMode = new ChoiceGroup("Режим отображения", Choice.POPUP);
    	viewMode.append("Полный", null);
    	viewMode.append("Краткий", null);
    	viewMode.setSelectedIndex(Options.viewMode-Options.VIEW_MODE_, true);
    	form.append(viewMode);
    	
	other = new ChoiceGroup("", Choice.MULTIPLE);
	other.append("За один день",null);
	other.append("Граница текста",null);
	other.setSelectedIndex(0, Options.onlyOneDay);
	other.setSelectedIndex(1, Options.textBorder);
	form.append(other);

	textColor = new ChoiceGroup("Цвет текста", Choice.POPUP);
    	showColor(textColor, Options.textColor);
    	form.append(textColor);
    	
    	backColor = new ChoiceGroup("Цвет фона", Choice.POPUP);
    	showColor(backColor, Options.backColor);
    	form.append(backColor);
    	
    	reqInterval = new ChoiceGroup("Интервал запроса (ч)", Choice.POPUP);
    	int reqIndex = -1;
    	for (int i = 0; i < reqIntervals.length; i++)
    	{
    		reqInterval.append(Integer.toString(reqIntervals[i]), null);
    		if (reqIntervals[i] == Options.requestInterval) reqIndex = i; 
    	}
    	reqInterval.setSelectedIndex((reqIndex != -1) ? reqIndex : 3, true);
    	form.append(reqInterval);
    	
    	display.setCurrent(form);
    }
    
    public void commandAction(Command c, Displayable d)
    {
    	if (c == cmdSave)
    	{
    		Options.cityNum = Integer.parseInt(cityNumber.getString());
    		
    		Options.onlyOneDay = other.isSelected(0);
    		Options.textBorder = other.isSelected(1);
    		Options.textColor = getColor(textColor, Options.textColor);
    		Options.backColor = getColor(backColor, Options.backColor);
    		Options.requestInterval =
    			reqInterval.getSelectedIndex() != -1
    			? Integer.parseInt(reqInterval.getString(reqInterval.getSelectedIndex()))
    			: 6;
    			
    		Options.viewMode = viewMode.getSelectedIndex()+Options.VIEW_MODE_;
    		
    		ScrWeather.upplyOptions(true);
    	}
    	else if (c == cmdCancel)
    	{
    		ScrWeather.upplyOptions(false);
    	}
    }
}
